Newer
Older
moodlog_web / habit.php
0xRoM on 2 Apr 41 KB hopefully sorted?!
  1. <?php
  2.  
  3. function habit_update($word, $moodlog) {
  4. // Call habit_check_daily function
  5. $result = habit_check_daily($word, $moodlog);
  6.  
  7. // Get today's date
  8. $todayDate = date("Y-m-d");
  9.  
  10. // Read the existing mood log file
  11. $lines = file($moodlog);
  12.  
  13. // Check if habit_check_daily returns true
  14. if ($result) {
  15. // Iterate through each line in the file
  16. foreach ($lines as $key => $line) {
  17. // Extract the date from the line
  18. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $match);
  19.  
  20. if (!empty($match[1])) {
  21. $lineDate = $match[1];
  22.  
  23. // Check if the line date is today
  24. if ($lineDate === $todayDate) {
  25. // Remove the word along with the '#' symbol (case-insensitive)
  26. $modifiedLine = preg_replace("/#?$word\b/i", '', $line);
  27.  
  28. // Remove the whole line if there is nothing after the number
  29. if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2} \[\d+\]\s*$/', $modifiedLine)) {
  30. unset($lines[$key]);
  31. } else {
  32. $lines[$key] = $modifiedLine;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. // Save the modified lines back to the file
  39. file_put_contents($moodlog, implode("", $lines));
  40. } else {
  41. $foundToday = false;
  42.  
  43. // Check if there is a line with today's date
  44. foreach ($lines as $key => $line) {
  45. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $match);
  46.  
  47. if (!empty($match[1]) && $match[1] === $todayDate) {
  48. $foundToday = true;
  49.  
  50. // Remove the trailing newline
  51. $lines[$key] = rtrim($line);
  52.  
  53. // Append #$text to the line
  54. $lines[$key] .= " #$word";
  55.  
  56. // Re-add the newline
  57. $lines[$key] .= "\n";
  58.  
  59. break;
  60. }
  61. }
  62.  
  63. // If today's date is not found, prepend a new line
  64. if (!$foundToday) {
  65. // Prepend a new line with Y-m-d hh:mm [0] #$word
  66. $newLine = $todayDate . " " . date("H:i") . " [0] #$word\n";
  67. array_unshift($lines, $newLine);
  68. }
  69.  
  70. // Save the modified lines back to the file
  71. file_put_contents($moodlog, implode("", $lines));
  72. }
  73. }
  74.  
  75. function habit_cal_daily($filter){
  76. global $moodlog;
  77.  
  78. $fileContent = file_get_contents($moodlog);
  79. $lines = explode("\n", $fileContent);
  80. $dateOccurrences = [];
  81.  
  82. // Get the current date
  83. $currentDate = date('Y-m-d');
  84.  
  85. foreach ($lines as $line) {
  86. // Extract the date from the line
  87. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  88. $date = isset($matches[0]) ? $matches[0] : null;
  89.  
  90. // Check if the line contains the specified word as a hashtag
  91. $wordFound = (strpos($line, "#$filter") !== false);
  92.  
  93. // Populate the dateOccurrences array
  94. if ($date !== null) {
  95. // Calculate the difference in weeks
  96. $weekDifference = floor((strtotime($currentDate) - strtotime($date)) / (7 * 24 * 60 * 60));
  97.  
  98. // Check if the week is more than 24 weeks ago
  99. if ($weekDifference > 24) {
  100. break;
  101. }
  102.  
  103. if (!isset($dateOccurrences[$date])) {
  104. $dateOccurrences[$date] = 0;
  105. }
  106.  
  107. if ($wordFound) {
  108. $dateOccurrences[$date] = 1;
  109. }
  110. }
  111. }
  112.  
  113. // Remove entries with a value of 0
  114. $dateOccurrences = array_filter($dateOccurrences, function ($value) {
  115. return $value !== 0;
  116. });
  117.  
  118. // Convert keys to timestamps and print in a simple JSON format with surrounding {}
  119. $result = '{';
  120. foreach ($dateOccurrences as $date => $value) {
  121. $timestamp = strtotime($date);
  122. $result .= "\"$timestamp\":$value,";
  123. }
  124.  
  125. // Remove trailing comma
  126. $result = rtrim($result, ',');
  127.  
  128. $result .= '}';
  129.  
  130. echo $result;
  131. }
  132.  
  133. function habit_cal_weekly($filter){
  134. global $moodlog;
  135.  
  136. $fileContent = file_get_contents($moodlog);
  137. $lines = explode("\n", $fileContent);
  138. $dateOccurrences = [];
  139. $weeklyOccurrences = [];
  140.  
  141. foreach ($lines as $line) {
  142. // Extract the date from the line
  143. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  144. $date = isset($matches[0]) ? $matches[0] : null;
  145.  
  146. // Explode the line into words
  147. $words = explode(' ', $line);
  148.  
  149. // Check if the line contains the specified word as a hashtag
  150. $wordFound = false;
  151. foreach ($words as $wordInLine) {
  152. // Check if the word contains "#takeout" (case-insensitive)
  153. if (stripos($wordInLine, "#$filter") !== false) {
  154. $wordFound = true;
  155. //echo "found: $line\n";
  156. break;
  157. }
  158. }
  159.  
  160. // Populate the dateOccurrences array
  161. if (!isset($dateOccurrences[$date])) {
  162. $dateOccurrences[$date] = 0;
  163. }
  164. if ($date !== null && $wordFound) {
  165. $dateOccurrences[$date]++;
  166. }
  167.  
  168. // Output the line whether the word is found or not
  169. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  170. }
  171.  
  172. foreach ($dateOccurrences as $date => $count) {
  173. // Convert each date to the format $year-$weekNo
  174. $dateTime = new DateTime($date);
  175. $dateTime->modify('this week sunday'); // Set to Sunday of the current week
  176. $yearWeek = $dateTime->format('Y-m-W');
  177.  
  178. // Add +1 for each date converted into $year-$weekNo as a key
  179. if (isset($weeklyOccurrences[$yearWeek])) {
  180. if( $dateOccurrences[$date] == 1)
  181. $weeklyOccurrences[$yearWeek]++;
  182. } else {
  183. if($dateOccurrences[$date] == 1){
  184. $weeklyOccurrences[$yearWeek] = 1;
  185. }else{
  186. $weeklyOccurrences[$yearWeek] = 0;
  187. }
  188. }
  189. }
  190.  
  191. // Remove entries with a value of 0
  192. $weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) {
  193. return $value !== 0;
  194. });
  195. //print_r($weeklyOccurrences);
  196.  
  197. $result = '{';
  198. foreach ($weeklyOccurrences as $date => $value) {
  199. $result .= "\"$date\":$value,";
  200. }
  201. // Remove trailing comma
  202. $result = rtrim($result, ',');
  203. $result .= '}';
  204. echo $result;
  205. }
  206.  
  207. function habit_last_10_daily($filter, $filename){
  208. // Split the file content into an array of lines
  209. $fileContent = file_get_contents($filename);
  210. $lines = explode("\n", $fileContent);
  211. $dateOccurrences = [];
  212.  
  213. // Get the current date
  214. $currentDate = date('Y-m-d');
  215.  
  216. foreach ($lines as $line) {
  217. // Extract the date from the line
  218. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  219. $date = isset($matches[0]) ? $matches[0] : null;
  220.  
  221. // Check if the line contains the specified word as a hashtag
  222. $wordFound = (strpos($line, "#$filter") !== false);
  223.  
  224. // Populate the dateOccurrences array
  225. if ($date !== null) {
  226. // Calculate the difference in weeks
  227. $dayDifference = floor((strtotime($currentDate) - strtotime($date)) / ( 24 * 60 * 60));
  228.  
  229. // Check if the week is more than 24 weeks ago
  230. if ($dayDifference > 9) {
  231. break;
  232. }
  233.  
  234. if (!isset($dateOccurrences[$date])) {
  235. $dateOccurrences[$date] = 0;
  236. }
  237.  
  238. if ($wordFound) {
  239. $dateOccurrences[$date] = 1;
  240. }
  241. }
  242. }
  243.  
  244. // Remove entries with a value of 0
  245. //$dateOccurrences = array_filter($dateOccurrences, function ($value) {
  246. // return $value !== 0;
  247. //});
  248.  
  249. // Convert keys to timestamps and print in a simple JSON format with surrounding {}
  250. $i = 0;
  251. $dateOccurrences = array_reverse($dateOccurrences);
  252. foreach ($dateOccurrences as $date => $value) {
  253. $i++;
  254. $result["last_10_$i"] = $value;
  255. }
  256. return $result;
  257. }
  258.  
  259. function habit_last_10_weekly($filter, $filename){
  260. // Split the file content into an array of lines
  261. $fileContent = file_get_contents($filename);
  262. $lines = explode("\n", $fileContent);
  263.  
  264. $dateOccurrences = [];
  265. $weeklyOccurrences = [];
  266.  
  267. $currentDate = date('Y-m-d');
  268. $firstOccurrenceDate = null;
  269.  
  270. foreach ($lines as $line) {
  271. // Extract the date from the line
  272. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  273. $date = isset($matches[0]) ? $matches[0] : null;
  274.  
  275. // Explode the line into words
  276. $words = explode(' ', $line);
  277.  
  278. // Check if the line contains the specified word as a hashtag
  279. $wordFound = false;
  280. foreach ($words as $wordInLine) {
  281. // Check if the word contains "#takeout" (case-insensitive)
  282. if (stripos($wordInLine, "#$filter") !== false) {
  283. $wordFound = true;
  284. break;
  285. }
  286. }
  287.  
  288. // Populate the dateOccurrences array
  289. if ($date !== null) {
  290. // Calculate the difference in weeks
  291. // Check if the date is within the last 10 weeks
  292. if (strtotime($date) < strtotime('-11 weeks')) {
  293. continue;
  294. }
  295. }
  296. // Populate the dateOccurrences array
  297. if (!isset($dateOccurrences[$date])) {
  298. $dateOccurrences[$date] = 0;
  299. }
  300. if ($date !== null && $wordFound) {
  301. $dateOccurrences[$date]++;
  302. $firstOccurrenceDate = $date;
  303. }
  304.  
  305. // Output the line whether the word is found or not
  306. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  307. }
  308.  
  309. $weekPrevStart = new DateTime($firstOccurrenceDate);
  310. // Add one week to the date
  311. $weekPrevStart->modify('-1 week');
  312. // Format and print the new date
  313. $weekPrevStart2 = $weekPrevStart->format('Y-m-d');
  314.  
  315. foreach ($dateOccurrences as $date => $count) {
  316. // Convert each date to the format $year-$weekNo
  317. $dateTime = new DateTime($date);
  318. $dateTime->modify('this week sunday'); // Set to Sunday of the current week
  319. $yearWeek = $dateTime->format('Y-m-W');
  320.  
  321. // Add +1 for each date converted into $year-$weekNo as a key
  322. if (isset($weeklyOccurrences[$yearWeek])) {
  323. if( $dateOccurrences[$date] == 1)
  324. $weeklyOccurrences[$yearWeek]++;
  325. } else {
  326. if($dateOccurrences[$date] == 1){
  327. $weeklyOccurrences[$yearWeek] = 1;
  328. }else{
  329. $weeklyOccurrences[$yearWeek] = 0;
  330. }
  331. }
  332. if(strtotime($date) < strtotime($weekPrevStart2))
  333. $weeklyOccurrences[$yearWeek] = "x";
  334. }
  335.  
  336. // Remove entries with a value of 0
  337. //$weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) {
  338. // return $value !== 0;
  339. //});
  340. //print_r($dateOccurrences);
  341. //print_r($weeklyOccurrences);
  342.  
  343. // Convert keys to timestamps and print in a simple JSON format with surrounding {}
  344. $i = 0;
  345. $weeklyOccurrences = array_reverse($weeklyOccurrences);
  346. foreach ($weeklyOccurrences as $date => $value) {
  347. $i++;
  348. $result["last_10_$i"] = $value;
  349. }
  350. return $result;
  351. }
  352.  
  353. function habit_parse_file($fileContent) {
  354. $flattenedArray = array();
  355.  
  356. // Split the file content into lines
  357. $lines = explode("\n", $fileContent);
  358.  
  359. foreach ($lines as $line) {
  360. // Use regular expressions to extract relevant information
  361. preg_match('/\[([^\]]+)\]\[([^\]]+)\] (.+) - (.+)/', $line, $matches);
  362.  
  363. if (count($matches) == 5) {
  364. $category = $matches[1];
  365. $value = $matches[2];
  366. $activity = $matches[3];
  367. $description = $matches[4];
  368.  
  369. // Add to the flattened array
  370. $flattenedArray[] = array(
  371. 'Category' => $category,
  372. 'Value' => $value,
  373. 'Activity' => $activity,
  374. 'Description' => $description
  375. );
  376. }
  377. }
  378.  
  379. return $flattenedArray;
  380. }
  381.  
  382. function habit_check_daily($word, $filename) {
  383. // Split the file content into an array of lines
  384. $fileContent = file_get_contents($filename);
  385. $lines = explode("\n", $fileContent);
  386. $dateOccurrences = [];
  387.  
  388. foreach ($lines as $line) {
  389. // Extract the date from the line
  390. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  391. $date = isset($matches[0]) ? $matches[0] : null;
  392.  
  393. // Check if the line contains the specified word as a hashtag
  394. $wordFound = (strpos($line, "#$word") !== false);
  395.  
  396. // Populate the dateOccurrences array
  397. if ($date !== null) {
  398. if (!isset($dateOccurrences[$date])) {
  399. $dateOccurrences[$date] = 0;
  400. }
  401. if($wordFound){
  402. $dateOccurrences[$date] = 1;
  403. }
  404. }
  405. }
  406.  
  407. // Check streak
  408. $currentStreak = 0;
  409. $today = date('Y-m-d');
  410. //print_r($dateOccurrences);
  411.  
  412. while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) {
  413. $currentStreak++;
  414. $today = date('Y-m-d', strtotime($today . ' - 1 day'));
  415. }
  416.  
  417. if( $currentStreak >= 1){
  418. return true;
  419. }else{
  420. return false;
  421. }
  422. }
  423.  
  424. function habit_count_weekly($word, $filename) {
  425. $fileContent = file_get_contents($filename);
  426. $lines = explode("\n", $fileContent);
  427. $currentWeekNumber = (int)date("W");
  428. $wordCount = 0;
  429.  
  430. foreach ($lines as $line) {
  431. preg_match('/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/', $line, $matches);
  432.  
  433. if (!empty($matches)) {
  434. $lineDate = new DateTime($matches[1]);
  435. $lineWeekNumber = (int)$lineDate->format('W');
  436.  
  437. // Check if the line's week number is the same as the current week number
  438. if ($lineWeekNumber == $currentWeekNumber) {
  439. $wordCount += substr_count($line, "#$word");
  440. }
  441. }
  442. }
  443.  
  444. return $wordCount;
  445. }
  446.  
  447. function habit_get_stat_streak($word, $filename) {
  448. // Split the file content into an array of lines
  449. $fileContent = file_get_contents($filename);
  450. $lines = explode("\n", $fileContent);
  451. $dateOccurrences = [];
  452.  
  453. foreach ($lines as $line) {
  454. // Extract the date from the line
  455. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  456. $date = isset($matches[0]) ? $matches[0] : null;
  457.  
  458. // Check if the line contains the specified word as a hashtag
  459. $wordFound = (strpos($line, "#$word") !== false);
  460.  
  461. // Populate the dateOccurrences array
  462. if ($date !== null) {
  463. if (!isset($dateOccurrences[$date])) {
  464. $dateOccurrences[$date] = 0;
  465. }
  466. if($wordFound){
  467. $dateOccurrences[$date] = 1;
  468. }
  469. }
  470. }
  471.  
  472. // Check streak
  473. $currentStreak = 0;
  474. $today = date('Y-m-d');
  475.  
  476. while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) {
  477. $currentStreak++;
  478. $today = date('Y-m-d', strtotime($today . ' - 1 day'));
  479. }
  480.  
  481. return $currentStreak;
  482. }
  483.  
  484. function habit_get_stat_streak_week($word, $multiplier, $filename) {
  485. // Split the file content into an array of lines
  486. $fileContent = file_get_contents($filename);
  487. $lines = explode("\n", $fileContent);
  488. $dateOccurrences = [];
  489. $weeklyOccurrences = [];
  490. $first = "";
  491.  
  492. foreach ($lines as $line) {
  493. // Extract the date from the line
  494. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  495. $date = isset($matches[0]) ? $matches[0] : null;
  496.  
  497. // Explode the line into words
  498. $words = explode(' ', $line);
  499.  
  500. // Check if the line contains the specified word as a hashtag
  501. $wordFound = false;
  502. foreach ($words as $wordInLine) {
  503. // Check if the word contains "#takeout" (case-insensitive)
  504. if (stripos($wordInLine, "#$word") !== false) {
  505. $wordFound = true;
  506. $first = $date; // $first will end up containing the first instance of the word
  507. break;
  508. }
  509. }
  510.  
  511. // Populate the dateOccurrences array
  512. if ($date !== null && $wordFound) {
  513. if (!isset($dateOccurrences[$date])) {
  514. $dateOccurrences[$date] = 0;
  515. }
  516. $dateOccurrences[$date]++;
  517. }
  518. }
  519.  
  520. // Create weekly occurrences array
  521. foreach ($dateOccurrences as $date => $count) {
  522. // Convert each date to the format $year-$weekNo
  523. $dateTime = new DateTime($date);
  524. $dateTime->modify('this week sunday'); // Set to Sunday of the current week
  525. $yearWeek = $dateTime->format('Y-m-W');
  526.  
  527. // Add +1 for each date converted into $year-$weekNo as a key
  528. if (isset($weeklyOccurrences[$yearWeek])) {
  529. $weeklyOccurrences[$yearWeek]++;
  530. } else {
  531. $weeklyOccurrences[$yearWeek] = 1;
  532. }
  533. }
  534.  
  535. // Fill in missing weeks up to the current week
  536. $today = new DateTime();
  537. $currentDate = date('Y-m-d');
  538.  
  539. while (strtotime($currentDate) >= strtotime($first)) {
  540. $dateTime = new DateTime($currentDate);
  541. $dateTime->modify('this week sunday'); // Set to Sunday of the current week
  542. $yearWeek = $dateTime->format('Y-m-W');
  543. if (!isset($weeklyOccurrences[$yearWeek])) {
  544. $weeklyOccurrences[$yearWeek] = 0;
  545. }
  546. $currentDate = date('Y-m-d', strtotime($currentDate . ' -1 day'));
  547. }
  548.  
  549. $consecutiveCount = 0;
  550. $maxConsecutiveCount = 0;
  551.  
  552. ksort($weeklyOccurrences);
  553.  
  554. foreach ($weeklyOccurrences as $week => $value) {
  555. if ($value >= $multiplier) {
  556. $consecutiveCount++;
  557. $maxConsecutiveCount = max($maxConsecutiveCount, $consecutiveCount);
  558. } else {
  559. $consecutiveCount = 0; // Reset count if the value is less than the multiplier
  560. }
  561.  
  562. // If the current week's count is less than the multiplier and it's the current week, reset the streak
  563. if ($value < $multiplier && $week === date('Y-m-W')) {
  564. $maxConsecutiveCount = 0;
  565. }
  566. }
  567.  
  568. return ($maxConsecutiveCount >= $multiplier) ? $maxConsecutiveCount : 0;
  569. }
  570.  
  571. function habit_get_stat_missed($word, $filename) {
  572. // Split the file content into an array of lines
  573. $fileContent = file_get_contents($filename);
  574. $lines = explode("\n", $fileContent);
  575.  
  576. // Set variables
  577. $missedDays = 0;
  578. $currentDate = date('Y-m-d');
  579. $wordFound = false;
  580.  
  581. // Find the minimum date in the lines
  582. $minDate = null;
  583. foreach ($lines as $line) {
  584. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  585. $date = isset($matches[0]) ? $matches[0] : null;
  586.  
  587. if ($date !== null && ($minDate === null || $date < $minDate)) {
  588. $minDate = $date;
  589. }
  590. }
  591.  
  592. // Loop for each day
  593. while (true) {
  594. // Check if the word is found on any line for the current date
  595. foreach ($lines as $line) {
  596. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  597. $date = isset($matches[0]) ? $matches[0] : null;
  598.  
  599. $wordFound = $wordFound || ($date === $currentDate && strpos($line, "#$word") !== false);
  600. }
  601.  
  602. // If the word is found or we reach the beginning of the available dates, break the loop
  603. if ($wordFound || $currentDate < $minDate) {
  604. break;
  605. }
  606.  
  607. // Move to the previous day
  608. $missedDays++;
  609. $currentDate = date('Y-m-d', strtotime($currentDate . ' - 1 day'));
  610. }
  611.  
  612. return $wordFound ? $missedDays : 0;
  613. }
  614.  
  615. function habit_get_stat_missed_week($word, $multiplier, $filename) {
  616. // Split the file content into an array of lines
  617. $fileContent = file_get_contents($filename);
  618. $lines = explode("\n", $fileContent);
  619. $dateOccurrences = [];
  620. $weeklyOccurrences = [];
  621. $first = "";
  622.  
  623. foreach ($lines as $line) {
  624. // Extract the date from the line
  625. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  626. $date = isset($matches[0]) ? $matches[0] : null;
  627.  
  628. // Explode the line into words
  629. $words = explode(' ', $line);
  630.  
  631. // Check if the line contains the specified word as a hashtag
  632. $wordFound = false;
  633. foreach ($words as $wordInLine) {
  634. // Check if the word contains "#takeout" (case-insensitive)
  635. if (stripos($wordInLine, "#$word") !== false) {
  636. $wordFound = true;
  637. $first = $date;
  638. break;
  639. }
  640. }
  641.  
  642. // Populate the dateOccurrences array
  643. if (!isset($dateOccurrences[$date])) {
  644. $dateOccurrences[$date] = 0;
  645. }
  646. if ($date !== null && $wordFound) {
  647. $dateOccurrences[$date]++;
  648. }
  649.  
  650. // Output the line whether the word is found or not
  651. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  652. }
  653.  
  654. $currentDate = date('Y-m-d');
  655. while (strtotime($first) <= strtotime($currentDate)) {
  656. $dateTime = new DateTime($currentDate);
  657. $yearWeekKey = $dateTime->format('Y-W');
  658. $weeklyOccurrences[$yearWeekKey] = 0;
  659. //echo $yearWeekKey;
  660. $currentDate = date('Y-m-d', strtotime($currentDate . ' -1 day'));
  661. }
  662.  
  663. foreach ($dateOccurrences as $date => $count) {
  664. // Convert each date to the format $year-$weekNo
  665. $dateTime = new DateTime($date);
  666. $yearWeek = $dateTime->format('Y-W');
  667.  
  668. // Add +1 for each date converted into $year-$weekNo as a key
  669. if (isset($weeklyOccurrences[$yearWeek])) {
  670. if( $dateOccurrences[$date] == 1)
  671. $weeklyOccurrences[$yearWeek]++;
  672. } else {
  673. if($dateOccurrences[$date] == 1){
  674. $weeklyOccurrences[$yearWeek] = 1;
  675. }else{
  676. $weeklyOccurrences[$yearWeek] = 0;
  677. }
  678. }
  679. }
  680.  
  681. //print_r($dateOccurrences);
  682. //print_r($weeklyOccurrences);
  683.  
  684. $countZeros = 0;
  685.  
  686. foreach ($weeklyOccurrences as $value) {
  687. if ($value < $multiplier) {
  688. $countZeros++;
  689. } else {
  690. break; // Stop counting when a non-zero value is encountered
  691. }
  692. }
  693. return $countZeros;
  694. }
  695.  
  696. function habit_get_stat_top($word, $filename) {
  697. $fileContent = file_get_contents($filename);
  698.  
  699. // Split the file content into an array of lines
  700. $lines = explode("\n", $fileContent);
  701.  
  702. $dateOccurrences = [];
  703. $currentStreak = 0;
  704. $longestStreak = 0;
  705.  
  706. foreach ($lines as $line) {
  707. // Extract the date from the line
  708. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  709. $date = isset($matches[0]) ? $matches[0] : null;
  710.  
  711. // Check if the line contains the specified word as a hashtag
  712. $wordFound = (strpos($line, "#$word") !== false);
  713.  
  714. // Populate the dateOccurrences array
  715. if ($date !== null) {
  716. if (!isset($dateOccurrences[$date])) {
  717. $dateOccurrences[$date] = 0;
  718. }
  719. if($wordFound){
  720. $dateOccurrences[$date] = 1;
  721. }
  722. }
  723. }
  724.  
  725. foreach ($dateOccurrences as $date => $value) {
  726. if ($value == 1) {
  727. $currentStreak++;
  728. } else {
  729. $currentStreak = 0;
  730. }
  731.  
  732. // Update the longest streak if the current streak is greater
  733. $longestStreak = max($longestStreak, $currentStreak);
  734. }
  735.  
  736. //print_r($dateOccurrences);
  737. return $longestStreak;
  738. }
  739.  
  740. function habit_get_stat_top_week($word, $multiplier, $filename) {
  741. // Split the file content into an array of lines
  742. $fileContent = file_get_contents($filename);
  743. $lines = explode("\n", $fileContent);
  744. $dateOccurrences = [];
  745. $weeklyOccurrences = [];
  746.  
  747. foreach ($lines as $line) {
  748. // Extract the date from the line
  749. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  750. $date = isset($matches[0]) ? $matches[0] : null;
  751.  
  752. // Explode the line into words
  753. $words = explode(' ', $line);
  754.  
  755. // Check if the line contains the specified word as a hashtag
  756. $wordFound = false;
  757. foreach ($words as $wordInLine) {
  758. // Check if the word contains "#takeout" (case-insensitive)
  759. if (stripos($wordInLine, "#$word") !== false) {
  760. $wordFound = true;
  761. break;
  762. }
  763. }
  764.  
  765. // Populate the dateOccurrences array
  766. if (!isset($dateOccurrences[$date])) {
  767. $dateOccurrences[$date] = 0;
  768. }
  769. if ($date !== null && $wordFound) {
  770. $dateOccurrences[$date]++;
  771. }
  772.  
  773. // Output the line whether the word is found or not
  774. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  775. }
  776.  
  777. foreach ($dateOccurrences as $date => $count) {
  778. // Convert each date to the format $year-$weekNo
  779. $dateTime = new DateTime($date);
  780. $yearWeek = $dateTime->format('Y-W');
  781.  
  782. // Add +1 for each date converted into $year-$weekNo as a key
  783. if (isset($weeklyOccurrences[$yearWeek])) {
  784. if( $dateOccurrences[$date] == 1)
  785. $weeklyOccurrences[$yearWeek]++;
  786. } else {
  787. if($dateOccurrences[$date] == 1){
  788. $weeklyOccurrences[$yearWeek] = 1;
  789. }else{
  790. $weeklyOccurrences[$yearWeek] = 0;
  791. }
  792. }
  793. }
  794.  
  795. //print_r($dateOccurrences);
  796. //print_r($weeklyOccurrences);
  797.  
  798. $currentStreak = 0;
  799. $longestStreak = 0;
  800.  
  801. foreach ($weeklyOccurrences as $week => $value) {
  802. if ($value >= $multiplier) {
  803. // Increment the streak length if the value is greater than or equal to the multiplier
  804. $currentStreak++;
  805. } else {
  806. // Reset the streak length if the value is less than the multiplier
  807. $currentStreak = 0;
  808. }
  809.  
  810. // Update the longest streak if the current streak is longer
  811. $longestStreak = max($longestStreak, $currentStreak);
  812. }
  813. return $longestStreak;
  814. }
  815.  
  816. function habit_get_stat_top_missing($word, $filename) {
  817. $fileContent = file_get_contents($filename);
  818.  
  819. // Split the file content into an array of lines
  820. $lines = explode("\n", $fileContent);
  821.  
  822. // Find the earliest instance of the word
  823. $earliestDateWithWord = null;
  824. foreach ($lines as $line) {
  825. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  826. $date = isset($matches[0]) ? $matches[0] : null;
  827.  
  828. // Check if the line contains the specified word as a hashtag
  829. $wordFound = (strpos($line, "#$word") !== false);
  830.  
  831. if ($wordFound && ($earliestDateWithWord === null || $date < $earliestDateWithWord)) {
  832. $earliestDateWithWord = $date;
  833. }
  834. }
  835.  
  836. if ($earliestDateWithWord === null) {
  837. // No instance of the word found, return 0
  838. return 0;
  839. }
  840.  
  841. $currentDate = $earliestDateWithWord;
  842. $today = date('Y-m-d');
  843. $dateArray = [];
  844.  
  845. // Initialize date array with 0 values starting from the earliest date with the word
  846. while ($currentDate <= $today) {
  847. $dateArray[$currentDate] = 0;
  848. $currentDate = date('Y-m-d', strtotime($currentDate . ' + 1 day'));
  849. }
  850.  
  851. // Loop over the file and update dateArray
  852. foreach ($lines as $line) {
  853. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  854. $date = isset($matches[0]) ? $matches[0] : null;
  855.  
  856. // Check if the line contains the specified word as a hashtag
  857. $wordFound = (strpos($line, "#$word") !== false);
  858.  
  859. if ($date !== null && isset($dateArray[$date])) {
  860. // Update dateArray based on word occurrence
  861. if($wordFound)
  862. $dateArray[$date] = 1;
  863. }
  864. }
  865.  
  866. //print_r($dateArray);
  867.  
  868. // Find the longest streak of 0's in dateArray
  869. $currentStreak = 0;
  870. $longestStreak = 0;
  871.  
  872. foreach ($dateArray as $value) {
  873. if ($value === 0) {
  874. $currentStreak++;
  875. } else {
  876. $longestStreak = max($longestStreak, $currentStreak);
  877. $currentStreak = 0;
  878. }
  879. }
  880.  
  881. // Check for a streak at the end of the array
  882. $longestStreak = max($longestStreak, $currentStreak);
  883.  
  884. return $longestStreak;
  885. }
  886.  
  887. function habit_get_stat_year($word, $filename) {
  888. $fileContent = file_get_contents($filename);
  889.  
  890. // Split the file content into an array of lines
  891. $lines = explode("\n", $fileContent);
  892.  
  893. $daysWithWord = [];
  894.  
  895. foreach ($lines as $line) {
  896. // Extract the date from the line
  897. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  898. $date = isset($matches[0]) ? $matches[0] : null;
  899.  
  900. // Check if the line contains the specified word as a hashtag
  901. $wordFound = (strpos($line, "#$word") !== false);
  902.  
  903. if ($date !== null && date('Y', strtotime($date)) == date('Y')) {
  904. // Check if the date is in the current year
  905. if (!isset($daysWithWord[$date])) {
  906. $daysWithWord[$date] = $wordFound ? 1 : 0;
  907. } else {
  908. $daysWithWord[$date] += $wordFound ? 1 : 0;
  909. }
  910. }
  911. }
  912.  
  913. $daysCount = array_sum($daysWithWord);
  914.  
  915. return $daysCount;
  916. }
  917.  
  918. function habit_get_json($word, $moodlog, $habitlog){
  919. $fileContent = file_get_contents($habitlog);
  920. $resultArray = habit_parse_file($fileContent);
  921. $found = false;
  922.  
  923. foreach ($resultArray as $section) {
  924. $jsonArray = array();
  925. $jsonArray['align'] = "";
  926. $jsonArray['buttonClass'] = "";
  927. $jsonArray['buttonText'] = "";
  928. if($section['Category'][0] == "d" && $section['Activity'] == $word){
  929. $found = true;
  930. $jsonArray['freq'] = "d";
  931.  
  932. if($section['Category'][1] == "+"){
  933. $jsonArray['align'] = "good";
  934. $jsonArray['calCol'] = "#58e81b";
  935. $jsonArray['title1'] = "Streak";
  936. $jsonArray['title2'] = "Missed";
  937. $jsonArray['title3'] = "Top";
  938. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  939. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  940. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  941.  
  942. }
  943. if($section['Category'][1] == "-"){
  944. $jsonArray['align'] = "bad";
  945. $jsonArray['calCol'] = "#e81b1b";
  946. $jsonArray['title1'] = "Streak";
  947. $jsonArray['title2'] = "Total (Year)";
  948. $jsonArray['title3'] = "Top";
  949. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  950. $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog);
  951. $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  952. }
  953. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  954. if ($todayCheck) {
  955. $jsonArray['buttonClass'] = 'button-'.$jsonArray['align'];
  956. }else{
  957. if ($section['Category'][1] == "-")
  958. $jsonArray['buttonClass'] = 'button-done';
  959. }
  960. break;
  961. }
  962. if($section['Category'][0] == "w" && $section['Activity'] == $word){
  963. $found = true;
  964. $jsonArray['freq'] = "w";
  965.  
  966. $multiplier = $section['Category'][1];
  967. $jsonArray['align'] = "good";
  968. $jsonArray['title1'] = "Streak";
  969. $jsonArray['title2'] = "Missed";
  970. $jsonArray['title3'] = "Top";
  971. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  972. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  973. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  974. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  975. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  976. $jsonArray['weekSoFar'] = $weekCount;
  977. $jsonArray['weekGoal'] = $multiplier;
  978.  
  979. if( $weekCount < $multiplier )
  980. $jsonArray['buttonClass'] = 'button-yellow';
  981. if($todayCheck)
  982. $jsonArray['buttonText'] = $weekCount;
  983. if($weekCount >= $multiplier)
  984. $jsonArray['buttonClass'] = 'button-done';
  985. if( $todayCheck ) {
  986. $jsonArray['buttonClass'] = 'button-neut';
  987. if($weekCount >= $multiplier){
  988. $jsonArray['buttonClass'] = 'button-good';
  989. $jsonArray['buttonText'] = "";
  990. }
  991. }
  992. if($weekCount == 0)
  993. $jsonArray['buttonClass'] = '';
  994. break;
  995. }
  996. }
  997.  
  998. if($found)
  999. return json_encode($jsonArray);
  1000. return false;
  1001. }
  1002.  
  1003. function habit_get_json_all($moodlog, $habitlog) {
  1004. $fileContent = file_get_contents($habitlog);
  1005. $resultArray = habit_parse_file($fileContent);
  1006.  
  1007. $jsonWordsArray = array();
  1008.  
  1009. foreach ($resultArray as $section) {
  1010. $jsonArray = array();
  1011. $jsonArray['activity'] = $section['Activity'];
  1012. $jsonArray['align'] = "";
  1013. $jsonArray['buttonClass'] = "";
  1014. $jsonArray['buttonText'] = "";
  1015.  
  1016. if ($section['Category'][0] == "d") {
  1017. $jsonArray['freq'] = "d";
  1018.  
  1019. if ($section['Category'][1] == "+") {
  1020. $jsonArray['align'] = "good";
  1021. $jsonArray['calCol'] = "#58e81b";
  1022. $jsonArray['title1'] = "Streak";
  1023. $jsonArray['title2'] = "Missed";
  1024. $jsonArray['title3'] = "Top";
  1025. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  1026. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  1027. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  1028. } elseif ($section['Category'][1] == "-") {
  1029. $jsonArray['align'] = "bad";
  1030. $jsonArray['calCol'] = "#e81b1b";
  1031. $jsonArray['title1'] = "Streak";
  1032. $jsonArray['title2'] = "Total (Year)";
  1033. $jsonArray['title3'] = "Top";
  1034. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  1035. $jsonArray['result2'] = habit_get_stat_year($section['Activity'], $moodlog);
  1036. $jsonArray['result3'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  1037. }
  1038.  
  1039. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  1040. $last10 = habit_last_10_daily($section['Activity'], $moodlog);
  1041. foreach ($last10 as $key => $value) {
  1042. $jsonArray[$key] = "neut";
  1043. if ($section['Category'][1] == "+" && $value == 1)
  1044. $jsonArray[$key] = "green";
  1045. if ($section['Category'][1] == "+" && $value == 0)
  1046. $jsonArray[$key] = "red";
  1047. if ($section['Category'][1] == "-" && $value == 1)
  1048. $jsonArray[$key] = "red";
  1049. if ($section['Category'][1] == "-" && $value == 0)
  1050. $jsonArray[$key] = "green";
  1051. }
  1052.  
  1053. if ($todayCheck) {
  1054. $jsonArray['buttonClass'] = 'button-' . $jsonArray['align'];
  1055. }else{
  1056. //if ($section['Category'][1] == "+")
  1057. // $jsonArray['buttonClass'] = 'button-bad';
  1058. if ($section['Category'][1] == "-")
  1059. $jsonArray['buttonClass'] = 'button-done';
  1060. }
  1061. } elseif ($section['Category'][0] == "w") {
  1062. $jsonArray['freq'] = "w";
  1063.  
  1064. $multiplier = $section['Category'][1];
  1065. $jsonArray['align'] = "good";
  1066. $jsonArray['title1'] = "Streak";
  1067. $jsonArray['title2'] = "Missed";
  1068. $jsonArray['title3'] = "Top";
  1069. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  1070. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  1071. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  1072.  
  1073. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  1074. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  1075. $jsonArray['weekSoFar'] = $weekCount;
  1076. $jsonArray['weekGoal'] = $multiplier;
  1077.  
  1078. if ($weekCount < $multiplier) {
  1079. $jsonArray['buttonText'] = $weekCount;
  1080. $jsonArray['buttonClass'] = 'button-yellow';
  1081. } elseif ($weekCount >= $multiplier) {
  1082. $jsonArray['buttonClass'] = 'button-done';
  1083. if($todayCheck){
  1084. $jsonArray['buttonClass'] = 'button-good';
  1085. $jsonArray['buttonText'] = "";
  1086. }
  1087. }
  1088. if ($todayCheck) {
  1089. $jsonArray['buttonClass'] = 'button-neut';
  1090.  
  1091. if ($weekCount >= $multiplier) {
  1092. $jsonArray['buttonClass'] = 'button-good';
  1093. }
  1094. }
  1095. if($weekCount == 0)
  1096. $jsonArray['buttonClass'] = '';
  1097.  
  1098. $last10 = habit_last_10_weekly($section['Activity'], $moodlog);
  1099. foreach ($last10 as $key => $value) {
  1100. $jsonArray[$key] = "neut";
  1101. if ($value >= $multiplier)
  1102. $jsonArray[$key] = "green";
  1103. if ($value < $multiplier)
  1104. $jsonArray[$key] = "yellow";
  1105. if ($value === "x")
  1106. $jsonArray[$key] = "neut";
  1107. if ($value === 0)
  1108. $jsonArray[$key] = "red";
  1109. }
  1110. }
  1111.  
  1112. $jsonWordsArray[] = $jsonArray;
  1113. }
  1114.  
  1115. return json_encode($jsonWordsArray);
  1116. }
  1117.  
  1118. /***
  1119. * creating calendar stuff
  1120. */
  1121. // Function to get the ISO week numbers in a month
  1122. function getISOWeeksInMonth($year, $month) {
  1123. $firstDay = new \DateTime("$year-$month-01");
  1124. $lastDay = new \DateTime($firstDay->format('Y-m-t'));
  1125.  
  1126. // Calculate ISO week numbers for each day
  1127. $isoWeeks = [];
  1128. $currentDay = clone $firstDay;
  1129.  
  1130. while ($currentDay <= $lastDay) {
  1131. $isoWeek = $currentDay->format('W');
  1132. $isoWeeks[] = $isoWeek;
  1133. $currentDay->modify('+1 day');
  1134. }
  1135.  
  1136. return array_unique($isoWeeks);
  1137. }
  1138.  
  1139. // Function to check if a date is a Sunday
  1140. function isSunday($date) {
  1141. return $date->format('w') == 0;
  1142. }
  1143.  
  1144. function create_new_weekly_cal(){
  1145. $calHTML = "";
  1146. // Get current date
  1147. $currentDate = new \DateTime();
  1148. $currentDate->modify('this week sunday');
  1149. // Create an array to store months
  1150. $months = [];
  1151.  
  1152. // Loop through the current month to 23 months ago and store in the array
  1153. for ($i = 0; $i < 23; $i++) {
  1154. $year = $currentDate->format('Y');
  1155. $month = $currentDate->format('m');
  1156.  
  1157. // Get the ISO week numbers in the month
  1158. $isoWeeksInMonth = getISOWeeksInMonth($year, $month);
  1159.  
  1160. // Store the month data in the array
  1161. $months[] = [
  1162. 'year' => $year,
  1163. 'month' => $month,
  1164. 'isoWeeks' => $isoWeeksInMonth,
  1165. ];
  1166.  
  1167. // Move forward one month for the next iteration
  1168. $currentDate->sub(new DateInterval('P1M'));
  1169. }
  1170.  
  1171. // Reverse the array to display the oldest month first
  1172. $months = array_reverse($months);
  1173.  
  1174. // Create the grid
  1175. $calHTML .= '<div class="grid-container">';
  1176.  
  1177. // Loop through the months array to output the grid
  1178. foreach ($months as $monthData) {
  1179. $year = $monthData['year'];
  1180. $month = $monthData['month'];
  1181. $lastDay = date('t', strtotime("$year-$month-01"));
  1182. $isoWeeksInMonth = $monthData['isoWeeks'];
  1183.  
  1184. $calHTML .= '<div class="month">';
  1185. //echo "<h3>{$year}-{$month}</h3>";
  1186. $calHTML .= '<div class="week-container">';
  1187. // Keep the last ISO week if it's December and the last day is Sunday
  1188. if ($month === '12' && isSunday(new \DateTime("$year-$month-31"))) {
  1189. foreach ($isoWeeksInMonth as $isoWeek) {
  1190. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  1191. }
  1192. } else if( isSunday(new \DateTime("$year-$month-$lastDay")) ){
  1193. foreach ($isoWeeksInMonth as $isoWeek) {
  1194. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  1195. }
  1196. } else {
  1197. // Exclude the last ISO week when outputting weeks
  1198. $keys = array_keys($isoWeeksInMonth);
  1199. $lastKey = end($keys);
  1200. foreach ($isoWeeksInMonth as $isoWeekKey => $isoWeek) {
  1201. if ($isoWeekKey === $lastKey) {
  1202. continue;
  1203. }
  1204. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  1205. }
  1206. }
  1207.  
  1208. $calHTML .= '</div>';
  1209. $calHTML .= '</div>';
  1210. }
  1211.  
  1212. $calHTML .= '</div>';
  1213. return $calHTML;
  1214. }
  1215.  
  1216.  
  1217. ?>
Buy Me A Coffee